﻿#define DROPCREATEDATABASEALWAYS

namespace EntityFramework.FullText.Samples
{
    using EntityFramework.FullText.Samples.Models;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.Infrastructure.Annotations;
    using System.Data.Entity.Migrations;
    using System.Data.Entity.ModelConfiguration.Conventions;
    using System.Data.Entity.SqlServer;

    internal sealed class EntitiesConfiguration : DbConfiguration
    {
        public EntitiesConfiguration()
        {
            SetMetadataAnnotationSerializer(FullTextIndexedTableAnnotation.AnnotationName, () => new FullTextIndexedTableAnnotationSerializer());
            SetMigrationSqlGenerator("System.Data.SqlClient", () => new FullTextEnabledSqlGenerator());
            //SetContextFactory<Entities>(() => new Entities(
            //    @"Data Source=.\SQLEXPRESS;Initial Catalog=EntityFrameworkFTS.Samples;Integrated Security=True")
            //    .Create());
        }
    }

    internal sealed class Configuration : DbMigrationsConfiguration<Entities>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
    }

    public class Entities : DbContext
    {
        static Entities()
        {
            Database.SetInitializer(new DatabaseInitializer());
        }

        public DbSet<Document> Documents { get; set; }
    }

    #region IDatabaseInitializer

#if CREATEDATABASEIFNOTEXISTS
    internal class DatabaseInitializer : CreateDatabaseIfNotExists<SampleEntities>
#elif (DROPCREATEDATABASEALWAYS) || (!CREATEDATABASEIFNOTEXISTS && !DROPCREATEDATABASEALWAYS)
    internal class DatabaseInitializer : DropCreateDatabaseAlways<Entities>
#endif
    {
        protected override void Seed(Entities context)
        {
            context.Documents.AddRange(new[]{
                new Document()
                {
                    ID = 1,
                    Text = "document 1"
                },
                new Document()
                {
                    ID=2,
                    Text="document 2"
                }
            });
        }
    }

    #endregion
}
